package com.coollang.smartbra.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.maps.MapView;
import com.coollang.smartbra.MyAutoLayoutActivity;
import com.coollang.smartbra.R;
import com.coollang.smartbra.eventbus.CommonEvent;
import com.coollang.smartbra.fragment.RunningSettingFragment;
import com.coollang.smartbra.presenter.RunningActivityPresenter;
import com.coollang.smartbra.utils.ActivitySwitcher;
import com.coollang.smartbra.utils.GPSUtils;
import com.coollang.smartbra.utils.TVDrawableUtils;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * @Auther: Timmy
 * @Data: 2017/8/10
 * @Fuction: 跑步页面
 */

public class RunningActivity extends MyAutoLayoutActivity {

    private static final String TAG = RunningActivity.class.getSimpleName();
    @BindView(R.id.status_bar)
    View statusBar;
    @BindView(R.id.content)
    LinearLayout content;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.mapview)
    MapView mMapView;
    @BindView(R.id.tv_gps)
    TextView mTvGps;
    @BindView(R.id.run_gps_tips)
    TextView mTvRunGpsTips;
    @BindView(R.id.btn_map_scroll)
    Button mBtnMapScroll;
    @BindView(R.id.iv_back)
    ImageView mIvBack;
    public RunningActivityPresenter mActivityPresenter;

    private static final int ACCESS_RESULT = 888;
    public RunningSettingFragment mRunningSettingFragment;
    public boolean isRunningStart;

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);

        setContentView(R.layout.activity_running);
        ButterKnife.bind(this);
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(statusBar, this, content);
        initViews();
        EventBus.getDefault().register(this);

        // 此方法必须重写
        mMapView.onCreate(savedInstanceState);
    }

    private void initViews() {
        mTvTitle.setText(R.string.run_title);
        mTvTitle.setTextColor(getResources().getColor(R.color.black_80));

        // setFragment
        //设置下面布局
        mRunningSettingFragment = new RunningSettingFragment();
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.running_framelayout, mRunningSettingFragment)
                .commit();
    }

    @Override
    protected void initData() {
        mActivityPresenter = new RunningActivityPresenter(this);
    }

    // 弹出打开GPSdialog
    public void showOpenGPSDialog() {

        AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setTitle("定位未打开");
        dialog.setMessage("您需要在系统设置中打开GPS定位才能使用");
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel1), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 点击事件的回调方法
                finish();
            }
        });

        dialog.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.go_setting), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 去系统设置页面
                // 转到手机设置界面，用户设置GPS
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivityForResult(intent, ACCESS_RESULT); // 设置完成后返回到原来的界面
            }
        });
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        Button posiviveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
        posiviveButton.setTextColor(Color.parseColor("#F79444"));
        Button negativeButton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
        negativeButton.setTextColor(Color.parseColor("#000000"));
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();

        // 查看是否打开GPS开关
        if (GPSUtils.isOPen(this)) {
            mActivityPresenter.initMap(mMapView);
        } else {
            showOpenGPSDialog();
        }

        mMapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
        EventBus.getDefault().unregister(this);

        AMapLocationClient locationClient = mActivityPresenter.mLocationClient;
        if (null != locationClient) {
            locationClient.onDestroy();
        }
    }

    public void setsetGPSAccuracy(int level) {

        switch (level) {
            case 0:     // 无信号
                TVDrawableUtils.drawRight(mTvGps, R.drawable.gps_0);
                mTvRunGpsTips.setVisibility(View.VISIBLE);
                break;
            case 1:     // 一级信号
                TVDrawableUtils.drawRight(mTvGps, R.drawable.gps_1);
                mTvRunGpsTips.setVisibility(View.VISIBLE);
                break;
            case 2:     // 二级信号
                TVDrawableUtils.drawRight(mTvGps, R.drawable.gps_2);
                mTvRunGpsTips.setVisibility(View.GONE);
                break;
            case 3:     // 三级信号
                TVDrawableUtils.drawRight(mTvGps, R.drawable.gps_3);
                mTvRunGpsTips.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }


    @OnClick({R.id.btn_map_location, R.id.btn_map_scroll, R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_map_location:
                // 定位到当前位置
                mActivityPresenter.onClickLocation();
                break;
            case R.id.btn_map_scroll:
                // 底部伸缩打开
                mRunningSettingFragment.onScroll();
                break;
            case R.id.iv_back:
                if (isRunningStart) {
                    Toast.makeText(RunningActivity.this, "跑步正在进行中", Toast.LENGTH_SHORT).show();
                    return;
                }
                finish();
                break;
            default:
                break;
        }
    }

    public void onEventMainThread(CommonEvent event) {
        if (event.type == CommonEvent.START) {
            // 点击了 start 按钮,运动开始
            if (event.what == CommonEvent.REQUESTSUEECCD) {
                isRunningStart = true;
                mTvTitle.setText(R.string.isRunning);
                mBtnMapScroll.setVisibility(View.VISIBLE);
                mRunningSettingFragment.startRunning();
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (isRunningStart) {
            Toast.makeText(this, "跑步正在进行中", Toast.LENGTH_SHORT).show();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    // 跳转页面
    public void gotoShareMapActivity() {
        ActivitySwitcher.goShareMapAct(RunningActivity.this, mActivityPresenter.getList());
    }

    public void showFinishDialog() {
        AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setMessage(getString(R.string.no_rundata));
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.continued), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 点击事件的回调方法
            }
        });

        dialog.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.tofinish), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        Button posiviveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
        posiviveButton.setTextColor(Color.parseColor("#F79444"));
        Button negativeButton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
        negativeButton.setTextColor(Color.parseColor("#000000"));
    }
}
